// =============================================================================
//  STATS - Statistical Analysis Tools, v.0.01
//  Copyright (C) 2012  Marco Vettigli
// -----------------------------------------------------------------------------
//
// This program  is free software:  you can  redistribute it and/or  modify it
// under the terms of the  GNU General Public License as published by the Free
// Software Foundation,  either version 3 of the License,  or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY;  without even  the  implied  warranty  of  MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License  for
// more details.
//
// You  should  have received a copy of the  GNU General Public License  along
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// =============================================================================
//
// MAINWINDOW.H
// The class MainWindow is the main window of StatS graphical user interface.
//
// Author: Marco Vettigli
//
// =============================================================================

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

// == include directives =======================================================

#include <QMainWindow>
#include <QList>
#include "core/stable.h"
#include "core/stablewidget.h"

// == class definitions ========================================================

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

// == constructors and destructors =============================================

    // -- public ---------------------------------------------------------------
    // MainWindow::MainWindow(Qwidget*)
    //
    // Default constructor for MainWindow class.
    //
    // -------------------------------------------------------------------------
    explicit MainWindow(QWidget *parent = 0);

    // -- public ---------------------------------------------------------------
    // MainWindow::~MainWindow()
    //
    // Default destructor for MainWindow class.
    //
    // -------------------------------------------------------------------------
    ~MainWindow();

// == getters ==================================================================

// == setters ==================================================================

// == inspectors ===============================================================

// == accessor function declarations ===========================================

    // -- public ---------------------------------------------------------------
    // MainWindow::newTableWidget()
    //
    // The function creates a new STableWidget and underling STable object.
    // A pointer to newly created STable is returned and recorded in '_tables'
    // QList. The STableWidget is selected as current one.
    //
    // -------------------------------------------------------------------------
    StatS::STable* newTableWidget();

    // -- public ---------------------------------------------------------------
    // MainWindow::openTableWidget(StatS::STable*)
    //
    // The function creates a new STableWidget and bind it to an existing
    // STable object 'table'. The argument pointer 'table' is returned for
    // convenience and the newly created STableWidget is selected.
    //
    // -------------------------------------------------------------------------
    StatS::STable* openTableWidget(StatS::STable* table);

    // -- public ---------------------------------------------------------------
    // MainWindow::openTableWidget(const QString)
    //
    // The function returns a pointer to current STable object with name 'name'.
    // If the matching table doesn't exist, 0 pointer is returned.
    //
    // -------------------------------------------------------------------------
    StatS::STable* getTable(const QString name);

    // -- public ---------------------------------------------------------------
    // MainWindow::getCurrentTable()
    //
    // The function returns a pointer to STable object associated with
    // currently selected STableWidget in main QTabWidget. If no widget is
    // selected or the one selected is not a QTableWidget, 0 pointer is
    // returned.
    //
    // -------------------------------------------------------------------------
    StatS::STable* getCurrentTable();

    // -- public ---------------------------------------------------------------
    // MainWindow::getSelectedTable()
    //
    // The function returns a pointer to STable object associated with
    // currently selected item in main QTreeWidget. If no item is
    // selected or the one selected is not bound to a QTable object, 0 pointer
    // is returned.
    //
    // -------------------------------------------------------------------------
    StatS::STable* getSelectedTable();

    // -- public ---------------------------------------------------------------
    // MainWindow::getTableWidgetIndex(const QString)
    //
    // The function returns the index of STableWidget bound to STable object
    // with give name 'name' in the QTabWidget. If the STableWidget is not
    // open or doesn't exist, -1 is returned.
    //
    // -------------------------------------------------------------------------
    int getTableWidgetIndex(const QString name);

    // -- public ---------------------------------------------------------------
    // MainWindow::getTableWidget(StatS::STable*)
    //
    // The function returns a pointer to STableWidget bound to STable 'table'
    // in the main QTabWidget. By default the user can avoid to specify the
    // 'table' reference, if so the function will return the currently selected
    // STableWidget, if any. If the STableWidget is not open or doesn't
    // exist, or the selected widget is not an STableWidget, 0 pointer is
    // returned.
    //
    // -------------------------------------------------------------------------
    StatS::STableWidget* getTableWidget(StatS::STable* table = 0);

    // -- public ---------------------------------------------------------------
    // MainWindow::updateTablesList()
    //
    // The function updates the list in main QTreeWidget.
    //
    // -------------------------------------------------------------------------
    void updateTablesList();

// == variable declarations ====================================================

private:

    Ui::MainWindow *ui;             // user inteface pointer

    QList<StatS::STable*> _tables;  // list of opened tables    
    static int _untitledNumber;     // number of untitled tables

// == private function declarations ============================================

private slots:

    // -- private --------------------------------------------------------------
    // MainWindow::on_mainTree_doubleClicked(const QModelIndex&)
    //
    // The function handles double click on main QTreeWidget.
    //
    // -------------------------------------------------------------------------
    void on_mainTree_doubleClicked(const QModelIndex &index);

    // -- private --------------------------------------------------------------
    // MainWindow::on_mainTree_customContextMenuRequested(const QPoint&)
    //
    // The function handles context menu request on main QTreeWidget.
    //
    // -------------------------------------------------------------------------
    void on_mainTree_customContextMenuRequested(const QPoint &pos);

    // -- private --------------------------------------------------------------
    // MainWindow::on_mainTab_tabCloseRequested(int)
    //
    // The function handles tab closing on main QTabWidget.
    //
    // -------------------------------------------------------------------------
    void on_mainTab_tabCloseRequested(int index);

    // -- private --------------------------------------------------------------
    // MainWindow::on_mainTab_currentChanged(int)
    //
    // The function handles index changing on main QTabWidget.
    //
    // -------------------------------------------------------------------------
    void on_mainTab_currentChanged(int index);

// == actions handlers =========================================================

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionExit_triggered()
    //
    // The function exits main application.
    //
    // -------------------------------------------------------------------------
    void on_actionExit_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionNew_triggered()
    //
    // The function handles new table action.
    //
    // -------------------------------------------------------------------------
    void on_actionNew_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionOpen_triggered()
    //
    // The function handles open table action.
    //
    // -------------------------------------------------------------------------
    void on_actionOpen_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionSave_triggered()
    //
    // The function handles save table action.
    //
    // -------------------------------------------------------------------------
    void on_actionSave_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionClose_triggered()
    //
    // The function handles close table action.
    //
    // -------------------------------------------------------------------------
    void on_actionClose_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionChange_name_triggered()
    //
    // The function handles change name action.
    //
    // -------------------------------------------------------------------------
    void on_actionChange_name_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionCut_triggered()
    //
    // The function handles cut cell content action.
    //
    // -------------------------------------------------------------------------
    void on_actionCut_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionCopy_triggered()
    //
    // The function handles copy cell content action.
    //
    // -------------------------------------------------------------------------
    void on_actionCopy_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionPaste_triggered()
    //
    // The function handles paste from clipboard to cells action.
    //
    // -------------------------------------------------------------------------
    void on_actionPaste_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionCopy_triggered()
    //
    // The function handles clear action of cell content.
    //
    // -------------------------------------------------------------------------
    void on_actionClear_triggered();

    // -- private --------------------------------------------------------------
    // MainWindow::on_actionImport_csv_triggered()
    //
    // The function handles import CSV action.
    //
    // -------------------------------------------------------------------------
    void on_actionImport_csv_triggered();

    void on_actionScatter_Plot_triggered();
};

#endif // MAINWINDOW_H
